<?php    
    
    class theApp
    {
		static function &Start()
		{
			global $g_hObjects;
			static $m_hApp = NULL;
				 
			if(!isset($m_hApp))
			{
				$m_hApp = new theApp();					 
				$g_hObjects[] = $m_hApp;
				$m_hApp->OnInit();
			}
				
			return $m_hApp;
		}
		
		function OnInit()
		{    
			mb_internal_encoding(ENCODING);
			date_default_timezone_set(TIMEZONE);
			
			ob_implicit_flush(0);
			ob_start();
			
			
			$this->OnProcessRequest();
		}
		
		function OnDestroy()
		{    			
			$szEnc       = $_SERVER['HTTP_ACCEPT_ENCODING'];
			$szETag      = $_SERVER['REQUEST_TIME'];
			$szContent   = ob_get_contents();
	    	$iContentLen = ob_get_length();
	    		
	    	ob_end_clean();
	    	
	    	if(headers_sent($szFile,$iLine)) 
	    	{	
	    		return 'headers already sent in file'.$szFile.' in line '.$iLine; 
	    	}
	   		
	   		if(stripos($szEnc,'gzip') >= 0)
	    	{
	    		$szContent   = gzencode($szContent,6);
	    		$iContentLen = strlen($szContent); 
	    		
	    		
				header('Content-Encoding: gzip');
				header('Content-encoding: gzip');
				header('Vary: Accept-Encoding');
				header('Content-Length: '.$iContentLen);
				header('Etag: "'.$szETag.'"');
            
				echo $szContent;
		    }
		    else if(stripos($szEnc,'deflate') >= 0)
		    {
		    	$szContent   = gzdeflate($szContent,6);
	    		$iContentLen = strlen($szContent); 
	    		
	    		
	    		header('Content-Encoding: deflate');
				header('Content-encoding: deflate');
				header('Vary: Accept-Encoding');
				header('Content-Length: '.$iContentLen);
				header('Etag: "'.$szETag.'"');
				
				echo $szContent;
		    }
	    	else
	    	{
	    		header('Content-Length: '.$iContentLen);
	    		header('Etag: "'.$szETag.'"');
	    		
				echo $szContent;
	    	}
		}
		
		/* processing user request */
		function OnProcessRequest()
		{
			header('Content-type: text/html; charset='.ENCODING);

			$szRequestType = CRequest::Type();
			$szUrlArray    = CRequest::GetRequestedUrlAsArray();
			$szModule      = $szUrlArray[0];
				
			//module name should be lowercase
			$szModuleName = strtolower(basename($szModule));
			$szModuleDir  = MODULE_DIR.'/'.$szModuleName;
			$szModulePath = $szModuleDir.'/'.$szModuleName.'.class.php';
		
			if(!is_file($szModulePath))
			{
				CHttp::Redirect(URL);
				return false;
			}
			
			include($szModulePath);
			
			$szModuleClassName = 'C'.ucfirst($szModuleName).'Module';
			$hModule = new $szModuleClassName();

			//for destructor
			$g_hObjects[] = &$hModule;
			
			//module initialization
			$hModule->OnInit();

			array_shift($szUrlArray);
			
			if(empty($szUrlArray[0]))
				$szUrlArray[0] = "";
			
			$szLang = CRequest::GetLanguage();
			
			if ( defined('SSL_MODE') && SSL_MODE === true ) {
			//uruchamianie SSL 
				if (CRequest::IsSSL() !== true ) CRequest::ForceSSL();
			}
				
			$hModule->OnRequest($szRequestType,$szUrlArray,$szLang);
			

			return;
		}
		
		//druga wersja routera
		//jesli chcesz podmienic - w OnInit() zamieniasz OnProcessRequest() na OnProcessRequest2()
		//nie powinno rozwalic serwisu
		function OnProcessRequest2()
		{
			$szRequestType = CRequest::Type();
				
			CRequest::GetRequestedUrlAsArray2($aModules,$aParams);
				
			$szModule = $aModules[0];
				
			//module name should be lowercase
			$szModuleName = strtolower(basename($szModule));
			$szModuleDir  = MODULE_DIR.'/'.$szModuleName;
			$szModulePath = $szModuleDir.'/'.$szModuleName.'.class.php';
		
			if(!is_file($szModulePath))
			{
				CHttp::Redirect(URL);
				return false;
			}
			
			include($szModulePath);
			
			$szModuleClassName = 'C'.ucfirst($szModuleName).'Module';
			$hModule = new $szModuleClassName();

			//for destructor
			$g_hObjects[] = &	$hModule;
				
			//module initialization
			$hModule->OnInit();	
			
			array_shift($aModules);
			
			if(empty($aModules[0]))
				$aModules[0] = '';
			
			//pobieramy jezyk
			$szLang = CRequest::GetLanguage();
				
			//odpalamy OnRequest danego modulu
			//zamienilem kolejnosc $aParams <=> $szLang
			$hModule->OnRequest($szRequestType,$aModules,$aParams,$szLang);
			
			
		}
    }
?>